package com.cci.kangdao.dao;

import com.cci.kangdao.dao.model.WorkOrderPics;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mybatis.spring.annotation.MapperScan;
import java.util.List;
import java.util.Map;

@MapperScan
public interface WorkOrderPicsDao {

    /*@Select("SELECT wop.Description ,wop.PictureUrl ,wop.TakeTime ,wop.Location ,wop.LocLat ,wop.LocLon FROM WorkOrderPics_T wop\n" +
           "INNER JOIN WorkOrder_T wo\n" +
           "ON wop.OrderId = wo.ID\n" +
           "WHERE wo.OrderNo = #{orderNo} AND  wop.Flag = 0")*/
	@Results({
		@Result(column="Description",property="description"),
		@Result(column="PictureUrl",property="picUrl"),
		@Result(column="TakeTime",property="shootingTime"),
		@Result(column="Location",property="shootingLocation"),
			@Result(column="LocLon",property="shootingLat"),
			@Result(column="LocLat",property="shootingLon"),
		})
    @Select("select t.* from (\n" +
                "SELECT wop.Description ,wop.PictureUrl ,wop.TakeTime ,wop.Location ,wop.LocLat ,wop.LocLon, wop.PicNo,row_number() over (partition by wop.picNo order by wop.id desc) as counts FROM WorkOrderPics_T wop\n" +
                "INNER JOIN WorkOrder_T wo\n" +
                "ON wop.OrderId = wo.ID\n" +
                "WHERE wo.OrderNo = #{orderNo} AND  wop.Flag = 0\n" +
            ") t\n" +
            "where t.counts = 1")
	public List<Map<String,Object>> getWorkOrderPics(Map<String,Object> map);


	/*@Select("SELECT wop.Description as description,wop.PictureUrl as picUrl,wop.TakeTime as shootingTime,wop.Location as shootingLocation,wop.LocLat as shootingLon,wop.LocLon as shootingLat,wop.PicNo FROM WorkOrderPics_T wop\n" +
			"INNER JOIN WorkOrder_T wo\n" +
			"ON wop.OrderId = wo.ID\n" +
			"WHERE wo.OrderNo = #{orderNo} AND  wop.Flag = 0")*/
	@Select("select t.* from (\n" +
            "SELECT wop.Description as description,wop.PictureUrl as picUrl,wop.TakeTime as shootingTime,wop.Location as shootingLocation,\n" +
                "wop.LocLat as shootingLon,wop.LocLon as shootingLat,wop.PicNo, row_number() over (partition by wop.picNo order by wop.id desc) as counts FROM WorkOrderPics_T wop\n" +
                "INNER JOIN WorkOrder_T wo\n" +
                "ON wop.OrderId = wo.ID\n" +
                "WHERE wo.OrderNo = #{orderNo} AND  wop.Flag = 0\n" +
            ") t\n" +
            "where t.counts = 1")
	public List<Map<String,Object>> getXCECWorkOrderPics(Map<String,Object> map);

	@Select("select * from WorkOrderPics_T where Flag = 0 and OrderId = #{orderId}")
	List<WorkOrderPics> getWorkOrderPictureListByOrderId(Long orderId);
	
	@Select("select top 1 * from WorkOrderPics_T where Flag = 0 and OrderId = #{orderId} order by CreateTime desc")
	WorkOrderPics selectOneByOrderId(Long orderId);

	@Update("update WorkOrderPics_T set Flag = -1,Deleter = #{deleter},DeleteTime = #{deleteTime} where OrderId = #{orderId} and PicNo = #{picNo}")
	int disablePicture(@Param("orderId") Long orderId,@Param("picNo") String picNo,@Param("deleter") Long deleter,@Param("deleteTime") String deleteTime);

    @Select("select PictureUrl from WorkOrderPics_T where OrderId = #{orderId} and Flag = 0")
	List<String> getPicUrlByOrderId(@Param("orderId") long orderId);

	@Update("update WorkOrderPics_T set Flag = #{flag},Deleter = #{deleter},DeleteTime = #{deleteTime} where OrderId = #{orderId} and PicNo like '%'+#{picNo}")
	int deteleFaultPicture(@Param("orderId") Long orderId,@Param("picNo") String picNo,@Param("deleter") Long deleter,@Param("deleteTime") String deleteTime, @Param("flag") int flag);
}
